import java.util.Random;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 30318
 * Date: 2023-05-13
 * Time: 8:31
 */
public class Test {

    //求2个正数的最大公约数
    public static void main(String[] args) {
        int a = 18;
        int b = 24;
        int c = a % b;//18  6
        while (c != 0) {
            a = b;//a = 24  18
            b = c;//b = 18  6
            c = a%b;//6 0
        }
        System.out.println(b);//6
    }

    //方法三
    public static void main18(String[] args) {
        for(int n = 1; n <= 100; n++){
            //int n = 11;
            int i;
            for (i = 2; i <= Math.sqrt(n); i++) {
                if(n % i == 0) {
                    //System.out.println(n + "不是素数");
                    break;
                }
            }
            if(i > Math.sqrt(n)) {
                System.out.println(n + "是素数");
            }
        }
    }

    //方法二
    public static void main17(String[] args) {
        int n = 9;
        int i;
        for (i = 2; i <= n/2; i++) {
            if(n % i == 0) {
                System.out.println(n + "不是素数");
                break;
            }
        }
        if(i > n/2) {
            System.out.println(n + "是素数");
        }
    }

    //判定素数
    //方法一
    public static void main16(String[] args) {
        int n = 9;
        int i;
        for (i = 2; i < n; i++) {
            if(n % i == 0) {
                System.out.println(n + "不是素数");
                break;
            }
        }
        if(i >= n) {
            System.out.println(n + "是素数");
        }
    }

    public static void main15(String[] args) {
        int n = 7;
        for (int i = 31; i >= 1; i -= 2) {
            System.out.println(((n>>i) & 1) + " ");
        }//偶数
        System.out.println();
        for (int i = 30; i >= 0; i -= 2) {
            System.out.println(((n>>i) & 1) + " ");
        }//奇数
    }

    public static void main14(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = 3;
        while(count != 0){
            System.out.println("请输入你的密码，共有"+count+"次机会！");
            String pass = scanner.nextLine();
            //if(pass == "123"){这个判断相等是错误的
            if(pass.equals("123")){//equals
                System.out.println("登陆成功");
                break;
            }else{
                System.out.println("登陆失败！");
            }
            count--;
        }
    }

    public static void main13(String[] args) {
        int n = 123;
        while(n != 0){
            System.out.println(n%10);
            n /= 10;
        }
    }

    //乘法口诀表
    public static void main12(String[] args) {
        for (int i = 1; i <= 9 ; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i+"*"+j +"="+ i*j +" ");
            }
            System.out.println();
        }
    }

    //打印X图形
    public static void main11(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNextInt()) {
            int n = scan.nextInt();
            for(int i = 0;i < n;i++) {
                for(int j = 0;j < n;j++) {
                    if((i == j) || (i+j == n-1)) {
                        System.out.print("*");
                    }else{
                        System.out.print(" ");
                    }
                }
                System.out.println();
            }
        }
    }

    //水仙花数
    public static void main10(String[] args) {
        for(int i = 1; i <= 999999; i++){
            int count = 0;
            int tmp = i;
            int sum = 0;
            while(tmp != 0){
                tmp /= 10;
                count++;
            }
            //count里面就存储了当前数字i是几位数
            tmp = i;
            while(tmp != 0){
                sum += Math.pow(tmp%10, count);
                tmp /= 10;
            }
            //sum就存储了和
            if(sum == i){
                System.out.println(i);
            }
        }
    }

    //计算1/1-1/2+1/3-1/4+1/5......+1/99-1/100
    public static void main9(String[] args) {
        double sum = 0.0;
        int flag = 1;
        for(int i = 1; i <= 100; i++){
            sum = sum + 1.0/i * flag;
            flag = -flag;
        }
        System.out.println(sum);
    }

    //方法三
    //一次循环：n=7  n=n&(n-1) = 7 & 6 = 6
    //第二次循环：n=6  n=n&(n-1)= 6 & 5= 4
    //第三次循环：n=4  n=n&(n-1)=4 & 3= 0
    public static void main8(String[] args) {
        int n = -1;
        int count = 0;
        while (n != 0) {
            n = n & (n-1);//有几个1有与几次
            count++;
        }
        System.out.println(count);
    }

    //方法二
    public static void main7(String[] args) {
        int n = -1;
        int count = 0;
        while(n != 0){//如果移动的过程当中是0了，就结束循环
            if((n & 1) != 0){
                count++;
            }
            n = n >>> 1;
        }
        System.out.println(count);
    }

    //求二进制1的个数
    //方法一
    public static void main6(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = -1;
        int count = 0;
        for (int i = 0; i < 32; i++) {
            if(((n>>i) & 1) == 1) {
                count++;
            }
        }
        System.out.println(count);
    }

    //猜数字游戏
    public static void main5(String[] args) {
        Random random = new Random();
        int n = random.nextInt(100);//[0-100)
        System.out.println("随机数"+n);
        Scanner scan = new Scanner(System.in);

        while(true){
            System.out.println("请输入你要猜的数字：");
            int num = scan.nextInt();
            if(num < n){
                System.out.println("猜小了!");
            }else if(num > n){
                System.out.println("猜大了！");
            }else{
                System.out.println("猜对了！");
                break;
            }
        }
    }

    //Java 如何生成一个简单的随机数
    public static void main4(String[] args) {
        Random random = new Random();
        //Random random = new Random(132);//固定值
        //int n = random.nextInt();
        int n = random.nextInt(100);//[0-100)
        //int n = random.nextInt(100)+1;//[0-101)
        //int n = random.nextInt(50)+50;//[0-50)+50 -> [50-100)
        //int n = random.nextInt(102);//[0-101]
        System.out.println(n);

        //Math.random();
    }

    public static void main3(String[] args) {
        Scanner scan = new Scanner(System.in);

        /*while(scan.hasNextInt()){
            System.out.println("输入你的年龄：");
            int age = scan.nextInt();
            System.out.println("年龄： " + age);
        }*/

        while(scan.hasNextLine()){
            System.out.println("输入你的姓名：");
            String name = scan.nextLine();
            System.out.println("姓名：" + name);
        }
        //在cmd命令行窗口，结束的时候使用ctr+z
    }

    public static void main2(String[] args) {
        //alt + 回车      ctr+鼠标左键    System.in:键盘获取
        Scanner scan = new Scanner(System.in);
        //int a = 10;
        String name = scan.nextLine();//可以读一整行
        //如果使用的是next，那么读到空格就会结束
        System.out.println("姓名： "+name);

        System.out.println("输入你的年龄: ");
        int age = scan.nextInt();
        System.out.println("年龄： "+age);

        System.out.println("输入你的工资");
        float money = scan.nextFloat();
        System.out.println("工资: "+money);

        scan.close();
    }

    public static void main1(String[] args) {
        System.out.println(10);//输出且换行
        System.out.print("不换行");
        System.out.printf("%s","这是格式化输出！");
    }
}
